package com.airshare;
import java.io.File;



import com.airshare.AirshareService;

public class Main {
	
	private String shareDirPath;
	private FileServer fs;
	private ShareDir shareDirectory;
	private AirshareService shareService;

	public Main() {
	}
	
	private void mkShareDirectory() {
		
		shareDirPath = System.getProperty("user.home") + System.getProperty("file.separator") + "airshare";
		
		File f = new File(shareDirPath);
		
		if (f.exists() && !f.isDirectory()) {
			// TODO: ask user for new share directory path!
			System.out.println(shareDirPath + ": file exists! Please rename or delete.");
		}

		if (f.exists() && f.isDirectory())
			return;
		else {
			f.mkdir();
		}
	}
	
	public void start() {
		mkShareDirectory();
		
		shareDirectory = new ShareDir(shareDirPath);

		// Start file server
		fs = new FileServer(System.getProperty("user.name"), shareDirectory);
		fs.start();	

		// Start UI
		UI ui = new UI(System.getProperty("user.name"), shareDirectory, this);
		
		// Start service discovery
		shareService = new AirshareService(ui);

		// Register our service
		shareService.register(System.getProperty("user.name"), fs.getPort());
	}
	
	public void stop() {

		// unregister service
		shareService.unregister(System.getProperty("user.name"));
		
		// stop the file server
		fs.kill();
		
		System.exit(0);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Main app = new Main();
		app.start();
	}
}
